$(function() {
	var car_params = {
					responsive: true,
					width: '100%',
					height: 94,
					auto: false,
					scroll: {
						duration: 150,
					},
					items: {
						width: 160,
						height: 80,
						visible: {
							max: 'auto'
						}
					},
					wipe: {
						onTouch: true,
						onMouse: true
					},
					mousewheel: {
						items: 1
					}
	};
	
	$('.b-elements').carouFredSel(car_params);
	
	$('.mini-prew').carouFredSel({
					responsive: true,
					width: '40%',
					height: 36,
					auto: false,
					scroll: {
						duration: 150,
					},
					items: {
						width: 77,
						height: 36,
						visible: {
							min: 1,
							max: 'auto'
						}
					},
					wipe: {
						onTouch: true,
						onMouse: true
					},
					mousewheel: {
						items: 1
					}
	});

	
	$(window).resize(resizeActivePage);
	
	//Backbone--------------------------------------------------------------------------
	
	var Controller = Backbone.Router.extend({
		routes: {
			"": "main",
			"!/": "main",
			"!/first": "first",
			"!/second": "second",
			"!/category/:cat": "category"
        },
		
		main: function() {
			/*$('.all_pages_block').hide("slide", { direction: "left" }, 1500);
			$('#main').show("slide", { direction: "right" }, 1500);*/
			/*$('.all_pages_block').slideUp(1500);
			$('#main').slideDown(1500);*/
			
			//$('#main').css({display:'block'});
			//$('#all_blocks').animate({left:'+' + $('#all_blocks').css('width')});
			/*$('.activePage').css({display:"none"});
			$('.activePage').removeClass('activePage');
			$('#main').addClass('activePage');*/
			
			if (!$('#main').hasClass('activePage'))
			{
				$('.activePage').css({zIndex:"100"});
				$('.activePage').removeClass('activePage');
				$('#main').addClass('activePage');
				$('.activePage').css({zIndex:"101"});
				$('#all_pages').animate({left: 0}, 1500);
			}
		},
		
		first: function() {
			/*$('.all_pages_block').hide("slide", { direction: "left" }, 1500);
			$('#first').show("slide", { direction: "right" }, 1500);*/
			/*$('.all_pages_block').slideUp(1500);
			$('#first').slideDown(1500);*/
			
			$('.activePage').css({zIndex:"100"});
			$('.activePage').removeClass('activePage');
			$('#first').addClass('activePage');
			$('.activePage').css({zIndex:"101"});
			$('#all_pages').animate({left:'-' + $('#all_pages').css('width')}, 1500);
			$('#first').css('width', '100%');
		},
		
		second: function() {
			/*$('.all_pages_block').hide("slide", { direction: "left" }, 1500);
			$('#second').show("slide", { direction: "right" }, 1500);*/
			/*$('.all_pages_block').slideUp(1500);
			$('#second').slideDown(1500);*/
			
			//$('#second').css({display:'block'});
			//$('#all_blocks').animate({left:'+' + $('#all_blocks').css('width')});
			/*$('.activePage').css({display:"none"});
			$('.activePage').removeClass('activePage');
			$('#second').addClass('activePage');*/
			
			$('.activePage').css({zIndex:"100"});
			$('.activePage').removeClass('activePage');
			$('#second').addClass('activePage');
			$('.activePage').css({zIndex:"101"});
			$('#all_pages').animate({left:'-' + $('#all_pages').css('width')}, 1500);
			$('#second').css('width', '100%');
		},
		
		category: function (cat) {
			var cat_class = '.' + cat;
			var classes = $(cat_class).attr('class');
			
			/*
			for (var i = 0; i <= 3; i++)
				console.log($(cat_class + ':last').after('<div class = "' + classes + '" />').next().html($(cat_class + ':first').html()));
			*/
				
			$('.loaded-uls .row-category').each(function() {
				$(this).html($(cat_class + ':first .caroufredsel_wrapper').html());
				$(this).attr('class', classes);
				$(this).children('ul').removeAttr('style');
				//$(this).carouFredSel(car_params);
			});
			
			$(cat_class + ':first').after($('.loaded-uls'));
			
			/*console.log(car_params.mousewheel.items);
			car_params.mousewheel.items = 1;
			
			car_params.synchronise = [
					[".loaded-uls ul", true],
					[cat_class + ':first', true]
				];*/

			//console.log(car_params.synchronise);
			
			
			$('.loaded-uls .row-title').remove();
			
			$('.loaded-uls').slideDown(1500);
			//if ($('.loaded-uls').nextAll('.row-category').size == 0)
			var uls = $('.loaded-uls').nextAll('.row-category').size();
			
			$('.loaded-uls').prev('.row-category').prevAll('.row-category').slideUp(1500);
			$('.loaded-uls').next('.row-category').nextAll('.row-category').slideUp(1500);
			
			/*if ($('.loaded-uls').nextAll('.row-category').size() == 0)
			{
				console.log($('.loaded-uls').prevAll('.row-category:first').html());
				$('.loaded-uls').after($('.loaded-uls').prevAll('.row-category:first').html());
				//$('.loaded-uls').slideDown(1500)
			}*/

			$('.loaded-uls .b-elements').carouFredSel(car_params);
			$(cat_class + ' .b-elements:first').trigger("synchronise", ".loaded-uls .b-elements");
			
			
			
			/*$(cat_class + ':first').carouFredSel({
				synchronise	: ".loaded-uls ul",
				auto	: false
			});
			
			$('.loaded-uls ul').carouFredSel({
				auto	: false
			});*/
			
			//car_params.synchronise = null;
		}
	});

	var controller = new Controller();

	Backbone.history.start();
});

function resizeActivePage()
{
	if (!$('#main').hasClass('activePage'))
		$('#all_pages').css('left', '-' + $('#all_pages').css('width'));
}